package listbyorder.access201_300.test239;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/27 10:55
 */
public class Solution2 {

    // 解法二： 使用优先级队列
    // 超时
    public int[] maxSlidingWindow(int[] nums, int k) {
        if (nums.length < 2) return nums;
        PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        int len = nums.length;
        int[] res = new int[len - k + 1];
        int index = 0;
        for (int i = 0; i < len; i++) {
            if (i >= k) {
                queue.remove(nums[i - k]);
            }
            queue.add(nums[i]);
            if (i >= k - 1) {
                res[index++] = queue.peek();
            }
        }
        return res;
    }
}
